Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make array parsing parameters error message consistency with ZPP failure #3429

Closed
wants to merge 1 commit into from
Closed

Make array parsing parameters error message consistency with ZPP failure #3429

wants to merge 1 commit into from

Conversation

carusogabriel
Copy link
Contributor

As suggested in #76674, we could improve our parsing parameters error messages for array_ functions, by exposing what was passed, instead of an array.

I'm opening the PR for review before merging, as well asking if this should go for 7.3 or wait for 7.4 :)

@cmb69
Copy link
Contributor

cmb69 commented Aug 5, 2018

In my opinion, this can go into PHP-7.3. (Minor issue: I'd like more consistency with ZPP failure messages, but wouldn't know how to accomplish this.)

@carusogabriel
Copy link
Contributor Author

@cmb69 Good idea. Maybe:

-Argument #2 should be an array, int given
+array_map() expects parameter 2 to be array, int given

Would be tricky to catch the function name for shared internal functions, but there must be a way.

@cmb69
Copy link
Contributor

cmb69 commented Aug 5, 2018

Hmm, maybe array_map(): Expected parameter 2 to be array, int given is good enough?

@carusogabriel
Copy link
Contributor Author

carusogabriel commented Aug 5, 2018

@cmb69 Also, should we throw a php_error_docref or a zend_internal_type_error, relying on strict_types?

@carusogabriel carusogabriel changed the title Improve error messages when parsing parameters in array functions Make array parsing parameters error message consistency with ZPP failure Aug 5, 2018
@cmb69
Copy link
Contributor

cmb69 commented Aug 6, 2018

@carusogabriel Hmm, not sure about the BC break – some may not like that.

@carusogabriel
Copy link
Contributor Author

@cmb69 I've kept the php_error_docref. Should be ready for the review.

@@ -6152,7 +6152,7 @@ PHP_FUNCTION(array_map)

for (i = 0; i < n_arrays; i++) {
if (Z_TYPE(arrays[i]) != IS_ARRAY) {
php_error_docref(NULL, E_WARNING, "Argument #%d should be an array", i + 2);
php_error_docref(NULL, E_WARNING, "Expected parameter #%d to be an array, %s given", i + 2, zend_get_type_by_const(Z_TYPE(arrays[0])));
Copy link
Contributor

@staabm staabm Aug 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# should be stripped here as well, or the other wayarround.

I liked the # personally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this one, thanks.

@@ -3,7 +3,7 @@ Test array_diff_key() function : usage variation - Passing unexpected values to
--FILE--
<?php
/* Prototype : array array_diff_key(array arr1, array arr2 [, array ...])
* Description: Returns the entries of arr1 that have keys which are not present in any of the others arguments.
* Description: Returns the entries of arr1 that have keys which are not present in any of the others arguments.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not submit unrelated whitespace changes! I fully agree that there shouldn't be any trailing whitespace, but automatically removing these may make diffs harder to read, and may cause merge conflicts. If at all, such whitespace changes should go to a separate commit/PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmb69 Gonna revert these changes, sorry. We do need to find a way to trim all those whitespaces in tests, btw

@@ -4639,7 +4639,7 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa

for (i = 0; i < argc; i++) {
if (Z_TYPE(args[i]) != IS_ARRAY) {
php_error_docref(NULL, E_WARNING, "Argument #%d is not an array", i + 1);
php_error_docref(NULL, E_WARNING, "Expected parameter %d to be an array, %s given", i + 1, zend_get_type_by_const(Z_TYPE(args[i])));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use zend_zval_type_name in some places and zend_get_type_by_const in others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used zend_zval_type_name when possible as was the function I found for such a task. But, as it needs a const zval *arg I got an error type when using here, for e.g.:

expected ‘const zval * {aka const struct _zval_struct *}’ but argument is of type ‘zval {aka struct _zval_struct}’

So I search a way to retrieve the type, and come up with zend_get_type_by_const + Z_TYPE. Is there a better way? :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get a zval* from a zval by writing &args[i] instead of args[i].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me and my C deficit 🤦‍♂️

@carusogabriel
Copy link
Contributor Author

@cmb69 May we merge this one?

@cmb69
Copy link
Contributor

cmb69 commented Aug 9, 2018

Maybe wait for a few days, so others are able to object. If nobody objects, it's fine to merge.

@carusogabriel
Copy link
Contributor Author

If there's no objection on this one, I'll merge on August 27th, so we can include it in PHP 7.3 beta 3.

@nikic
Copy link
Member

nikic commented Aug 19, 2018

@carusogabriel Feel free to merge right away, I think enough time has passed for anyone to object.

@carusogabriel
Copy link
Contributor Author

Merged as efbf846, thanks!

@carusogabriel carusogabriel deleted the improve-array-error-messages branch August 20, 2018 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants